import java.util.concurrent.atomic.AtomicInteger;

/**
 * ABA问题演示
 */
public class ABADemo1 {
    private static AtomicInteger money=new AtomicInteger(100);
    public static void main(String[] args) throws InterruptedException {
        //第一次点击转账按钮(-50)
        Thread t1=new Thread(()->{
            int  oldmoney=money.get();
            //执行花费2S
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            money.compareAndSet(oldmoney,oldmoney-50);
        });
        t1.start();

        Thread t2=new Thread(()->{
            int oldmoney=money.get();
            money.compareAndSet(oldmoney,oldmoney-50);
        });
        t2.start();

        //给账户+50
        Thread t3=new Thread(()->{
            //执行花费1s
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int oldmoney=money.get();
            money.compareAndSet(oldmoney,oldmoney+50);
        });
        t3.start();

        t1.join();
        t2.join();
        t3.join();
        System.out.println("最终账号余额:"+money.get());
    }
}
